<?php
  require(dirname(__FILE__).'/global.php');
  require(ROOT.'/admin/isLogin.php');
  
  $submit = $_POST['submit'];
  if(isset($submit))
  {
      $dels = $_POST['del'];
      $orderNos = $_POST['orderNo'];
      $names = $_POST['name'];
      $urls = $_POST['url'];
      $describes = $_POST['describe'];
      $links = array();     
      
      for($i = 0; $i < count($dels); $i++){
          $sql = "delete from talking_link where LinkID=".$dels[$i];
          $db->execute($sql);
      }
      
      for($i = 0; $i < count($names); $i++){
          $temp = array();
          if(empty($names[$i])||empty($urls[$i])) continue;
          $temp['orderNo'] =  empty($orderNos[$i]) ? 0 : $orderNos[$i];
          $temp['name'] = $names[$i];
          $temp['url'] = $urls[$i];
          $temp['describe'] = $describes[$i];
          $links[] = $temp;
      }
      
      foreach($links as $link){
          $sql = "insert into talking_link(LinkName,LinkURL,LinkDescribe,LinkOrderNo) values('".$link['name']."','".$link['url']."','".$link['describe']."',".$link['orderNo'].")";           
          $db->execute($sql);
      }
      $smarty->assign('tip','更新成功');
  }
  $sql = "select LinkID,LinkOrderNo,LinkName,LinkURL,LinkDescribe from talking_link order by LinkOrderNo";
  $links = $db->query($sql);
  $smarty->assign('links',$links);
  $smarty->display('link.html');
  $db->close();
?>
